import customtkinter as ctk

class toptitlebar(ctk.CTkFrame):
    def __init__(self,master,
                height=25,
                fg_color="#000000", 
                hover_color="#B70000",    # 悬停背景颜色
                text_color="#FFFFFF",      # 文字颜色
                enable_maximize=True,
                *args, **kwargs):
        super().__init__(master,height=height, fg_color=fg_color,*args, **kwargs)
        self.pack_propagate(False)
        self.title_height = height

        # 左侧内容容器
        self.left_container = ctk.CTkFrame(self, fg_color="transparent")
        self.left_container.pack(side="left", padx=5)

        # 右侧按钮容器
        self.right_container = ctk.CTkFrame(self, fg_color="transparent")
        self.right_container.pack(side="right", padx=5)

        # 添加按钮
        self.create_controls(fg_color, hover_color, enable_maximize)

        # 窗口拖动功能
        self.bind("<Button-1>", self.get_offset)
        self.bind("<B1-Motion>", self.move_window)

    def create_controls(self, button_color, hover_color, enable_maximize):
        # 关闭按钮
        self.close_button = ctk.CTkButton(self.right_container, text="✕", width=30,
                                          height=self.title_height - 5,
                                          fg_color=button_color,
                                          hover_color=hover_color,
                                          command=self.master.destroy)
        self.close_button.pack(side="left", padx=2)

        # 最大化按钮
        if enable_maximize:
            self.maximize_button = ctk.CTkButton(self.right_container, text="□", width=30,
                                                 height=self.title_height - 5,
                                                 fg_color="gray", hover_color="dark gray",
                                                 command=self.toggle_maximize)
            self.maximize_button.pack(side="left", padx=2)

        # 最小化按钮
        self.minimize_button = ctk.CTkButton(self.right_container, text="—", width=30,
                                             height=self.title_height - 5,
                                             fg_color="gray", hover_color="dark gray",
                                             command=self.master.iconify)
        self.minimize_button.pack(side="left", padx=2)

    def get_offset(self, event):
        self.offset_x = event.x_root - self.master.winfo_x()
        self.offset_y = event.y_root - self.master.winfo_y()

    def move_window(self, event):
        try:
            self.master.geometry(f"+{event.x_root - self.offset_x}+{event.y_root - self.offset_y}")
        except TclError:
            pass

    def toggle_maximize(self):
        if self.master.attributes("-zoomed"):
            self.master.attributes("-zoomed", False)
            self.master.geometry("800x600")  # 恢复默认大小
        else:
            self.master.attributes("-zoomed", True)

    def add_left_widget(self, widget):
        """添加自定义控件到左侧区域"""
        widget.pack(side="left", padx=2)